package com.ct.gd.passport.view.weather;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.BaseActivity;
import com.ct.gd.passport.common.constants.Config;
import com.ct.gd.passport.common.constants.Config.Area;
import com.ct.gd.passport.view.LocationFragment;
import com.ct.gd.passport.view.LocationFragment.OnLocationsLoadedListener;
import com.ct.gd.passport.view.weather.util.SinaWeatherUtil;
import com.ct.gd.passport.view.widget.CustomHeaderFragment;
import com.up91.common.android.connect.NetUtil;

public class WeatherServiceActivity extends BaseActivity implements UpdateWeatherTask.IResponseAble, OnClickListener,
		LocationFragment.OnLocationChangedListener, OnLocationsLoadedListener {
	private static final String		TAG_LOCATION_FRAGMENT	= "TAG_LOCATION_FRAGMENT";

	private LocationFragment		mFgLocation;
	private CustomHeaderFragment	mFgHeader;

	TextView						mTvCurrentStatus1;
	TextView						mTvCurrentStatus2;
	TextView						mTvCurrentTemperature1;
	TextView						mTvCurrentTemperature2;
	ImageView						mIvCurrentWeather;
	TextView						mTvCurrentDate;

	protected View					mBtnRight;											// 头部右边图标按钮

	Animation						mAnimReflash;
	PopupWindow						mPwSetting;

	GridView						mGvForecast;
	ForecastGridAdapter				mGridAdapter;
	UpdateWeatherTask				mUpdateWeatherTask;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.weather);
	}

	@Override
	protected void initViews() {
		mFgHeader = (CustomHeaderFragment) getSupportFragmentManager().findFragmentById(R.id.layout_header);
		//TODO
//		mBtnRight = getLayoutInflater().inflate(R.layout.weather_reload, mFgHeader.getRightRoot(), false);
		mBtnRight = getLayoutInflater().inflate(R.layout.weather_reload, (ViewGroup)mFgHeader.getView(), false);
		mBtnRight.setOnClickListener(this);
		mFgHeader.setRightView(mBtnRight);

		mFgLocation = new LocationFragment();
		getSupportFragmentManager().beginTransaction().add(mFgLocation, TAG_LOCATION_FRAGMENT).commit();

		mAnimReflash = AnimationUtils.loadAnimation(this, R.anim.reflashing);

		mTvCurrentStatus1 = (TextView) findViewById(R.id.home_tvStatus1);
		mTvCurrentStatus2 = (TextView) findViewById(R.id.home_tvStatus2);
		mTvCurrentTemperature1 = (TextView) findViewById(R.id.home_tvTemperature1);
		mTvCurrentTemperature2 = (TextView) findViewById(R.id.home_tvTemperature2);
		mIvCurrentWeather = (ImageView) findViewById(R.id.home_ivWeather);
		mTvCurrentDate = (TextView) findViewById(R.id.home_tvDate);

		mGvForecast = (GridView) findViewById(R.id.home_gvForecast);
		mGridAdapter = new ForecastGridAdapter();
		mGvForecast.setAdapter(mGridAdapter);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		mFgLocation.registerOnLocationChangedListener(this);
		mFgLocation.registerOnLocationsLoadedListener(this);
	}

	@Override
	public void onLocationChanged(Area area) {
		catchWeathers(area.name);
		mFgLocation.getLocationTextView().setText(area.name);
	}

	@Override
	public void onLocationsLoaded(List<Area> areas) {
		mFgHeader.setCenterView(mFgLocation.getView());
	}

	public void onClick(View view) {
		if (view == mBtnRight) catchWeathers(Config.currentArea.name);
	}

	/**
	 * 获取某城市5天内的天气
	 * 
	 * *在UI线程中使用
	 * 
	 * @param city
	 */
	public void catchWeathers(String city) {
		if (city == null || city.length() == 0) {
			Toast.makeText(this, R.string.NO_CITY, 3000).show();
			return;
		}
		if (NetUtil.isNetworkAvailable(this)) {
			mBtnRight.startAnimation(mAnimReflash);
			mUpdateWeatherTask = new UpdateWeatherTask(this);
			mUpdateWeatherTask.execute(city);
		} else {
			Toast.makeText(this, R.string.NO_CONNECT, 3000).show();
		}
	}

	/**
	 * 接收获取到的天气信息 from UpdateWeatherTask
	 */
	public void onResponse(WeatherCondition[] wcs) {
		mBtnRight.clearAnimation();
		WeatherCondition wcCurrent = wcs[0];
		if (wcCurrent == null) {
			String info = getResources().getString(R.string.CATCH_FAILURE, Config.currentArea.name);
			Toast.makeText(this, info, 1000).show();
		} else {
			setCurrentWeather(wcCurrent);
			int size = wcs.length;
			for (int i = 1; i < size; i++) {
				setForecastWeather(wcs[i], i - 1);
			}
			Toast.makeText(this, R.string.REFLASH_COMPLETE, 1000).show();
		}
	}

	/** 设置今天的天气 */
	private void setCurrentWeather(WeatherCondition wc) {
		if (wc == null) {
			return;
		}
		mTvCurrentStatus1.setText(wc.status1);
		mTvCurrentStatus2.setText(wc.status2);
		mTvCurrentTemperature1.setText("" + wc.temperature1 + "°");
		mTvCurrentTemperature2.setText("" + wc.temperature2 + "°");
		mTvCurrentDate.setText(wc.date);
		mIvCurrentWeather.setImageResource(SinaWeatherUtil.getWeatherResource(wc.figure1, true));
	}

	/**
	 * 设置将来的天气(底部天气)
	 * 
	 * @param wc
	 * @param day
	 */
	private void setForecastWeather(WeatherCondition wc, int day) {
		if (wc == null) {
			return;
		}
		ForecastGridAdapter.ForecastItem item = mGridAdapter.getItem(day);
		item.mTvDate.setText(wc.shortDate);
		item.mIvWeather.setImageResource(SinaWeatherUtil.getWeatherResource(wc.figure1, false));
		item.mTvTemperature.setText(wc.getTemperatureText());
	}

	/**
	 * 天气格子的数据适配
	 * 
	 * @author Administrator
	 * 
	 */
	public class ForecastGridAdapter extends BaseAdapter {
		public class ForecastItem {
			TextView	mTvDate;
			TextView	mTvTemperature;
			ImageView	mIvWeather;
		}

		public static final int	COUNT			= 4;
		public View[]			gridItemView	= new View[COUNT];
		public ForecastItem[]	gridItems		= new ForecastItem[COUNT];

		public int getCount() {
			return COUNT;
		}

		public ForecastItem getItem(int position) {
			return gridItems[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (gridItemView[position] == null) {
				ForecastItem item = new ForecastItem();
				convertView = getLayoutInflater().inflate(R.layout.weather_griditem, null);
				item.mTvDate = (TextView) convertView.findViewById(R.id.griditem_tvDate);
				item.mIvWeather = (ImageView) convertView.findViewById(R.id.griditem_ivWeather);
				item.mTvTemperature = (TextView) convertView.findViewById(R.id.griditem_tvTemperature);
				item.mTvDate.setText("??");
				item.mTvTemperature.setText("-/-");
				item.mIvWeather.setImageResource(SinaWeatherUtil.WEATHER_RESOURCE[0]);
				gridItems[position] = item;
				gridItemView[position] = convertView;
			}
			return gridItemView[position];
		}

	}

}